package com.scuec.trainingmanagementsys.mapper;

import com.scuec.trainingmanagementsys.entity.Grade;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GradeMapper {
    @Insert("INSERT INTO grade (ProjectID, TeamID, TeamMemberID, Score, Comments) " +
            "VALUES (#{projectId}, #{teamId}, #{teamMemberId}, #{score}, #{comments})")
    @Options(useGeneratedKeys = true, keyProperty = "gradeId")
    int insertGrade(Grade grade);

    @Select("SELECT g.*, p.ProjectName, t.TeamName, s.StudentName " +
            "FROM grade g " +
            "LEFT JOIN project p ON g.ProjectID = p.ProjectID " +
            "LEFT JOIN team t ON g.TeamID = t.TeamID " +
            "LEFT JOIN teammember tm ON g.TeamMemberID = tm.TeamMemberID " +
            "LEFT JOIN student s ON tm.StudentID = s.StudentID " +
            "WHERE g.ProjectID = #{projectId}")
    List<Grade> getGradesByProjectId(Integer projectId);

    @Update("UPDATE grade SET Score = #{score}, Comments = #{comments} " +
            "WHERE GradeID = #{gradeId}")
    int updateGrade(Grade grade);

    // 添加 @Param 注解
    @Select("SELECT g.*, p.ProjectName, t.TeamName, s.StudentName " +
            "FROM grade g " +
            "LEFT JOIN project p ON g.ProjectID = p.ProjectID " +
            "LEFT JOIN team t ON g.TeamID = t.TeamID " +
            "LEFT JOIN teammember tm ON g.TeamMemberID = tm.TeamMemberID " +
            "LEFT JOIN student s ON tm.StudentID = s.StudentID " +
            "WHERE g.ProjectID = #{projectId} AND tm.StudentID = #{studentId}")
    Grade getGradeByProjectAndStudent(@Param("projectId") Integer projectId, @Param("studentId") Integer studentId);

    @Select("SELECT g.*, p.ProjectName, t.TeamName " +
            "FROM grade g " +
            "LEFT JOIN project p ON g.ProjectID = p.ProjectID " +
            "LEFT JOIN team t ON g.TeamID = t.TeamID " +
            "WHERE g.ProjectID = #{projectId} AND g.TeamID = #{teamId}")
    Grade getGradeByProjectAndTeam(@Param("projectId") Integer projectId, @Param("teamId") Integer teamId);
}
